/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.editor.chassis;

import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JPopupMenu;

import twaver.Grid;
import twaver.TUIManager;
import twaver.TWaverUtil;
import twaver.network.TNetwork;
import twaver.network.toolbar.BaseNetworkButton;

public class GridButton extends BaseNetworkButton {

	private JPopupMenu menu = new JPopupMenu();

	public GridButton(final TNetwork network) {
		super(network);
		this.setToolTipText(TWaverUtil.getClassNameWithoutPackage(Grid.class));

		this.setIcon(TUIManager.getIcon(Grid.class));

		this.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				menu.removeAll();
				menu.add(new GridPopupPanel(GridButton.this));
				Point point = GridButton.this.getLocation();
				menu.show(GridButton.this.getParent(), point.x, (int) (point.y + getHeight()));
			}
		});
	}

	public void setSelectionCell(int row, int col) {
		Grid grid = new Grid();
		grid.setRowCount(row);
		grid.setColumnCount(col);
		grid.setWidthSize(col * 60);
		grid.setHeightSize(row * 30);
		network.getDataBox().addElement(grid);
		menu.setVisible(false);
	}

	public JPopupMenu getMenu() {
		return menu;
	}

	public void setMenu(JPopupMenu menu) {
		this.menu = menu;
	}
}